using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace p_Library
{
    /// <summary>
    /// Ma hoa hay giai ma bang phuong phap RijndaelManaged
    /// </summary>

    public class cls_CONCryptography
    {
        #region properties
        private string _key = "NGUYENKHOIVIET";
        /// <summary>
        /// Khoa ma hoa hay giai ma
        /// </summary>
        public string Key
        {
            set { _key = value; }
        }
        #endregion

        #region Var
        private string _vector = "1H5U4N1G4P7HUONG";
        private string _satValue = "@P-HRM:QUYNHMAI";
        private string _hashAlgorithm = "MD5";
        private int _interation = 5;
        private int _keySize = 128;
        #endregion
        
        /// <summary>
        /// Ma hoa chuoi
        /// </summary>
        /// <param name="_password"></param>
        /// <returns></returns>

        #region Encryptography
        public string mtd_Encrypt(string _password)
        {
            /*
             * Chuyen cac chuoi thanh kieu byte
             */
            byte[] _vectorBytes = Encoding.ASCII.GetBytes(_vector);
            byte[] _satBytes = Encoding.ASCII.GetBytes(_satValue);
            byte[] _pwdBytes = Encoding.UTF8.GetBytes(_password);

            /*
             * Tao ma (key dung lam ma hoa
             */
            PasswordDeriveBytes _pwd = new PasswordDeriveBytes(_key, _satBytes, _hashAlgorithm, _interation);
            byte[] _keyEncrypt = _pwd.GetBytes(_keySize / 8);

            /*
             * Ma hoa bang phuong phap RijndaelManaged 
             */
            byte[] _tempEncrypt = null;

            RijndaelManaged _rijndae = null;
            MemoryStream _stream = null;
            CryptoStream _cryptStream = null;
            try
            {
                _rijndae = new RijndaelManaged();
                _rijndae.Mode = CipherMode.CBC;
                ICryptoTransform _transform = _rijndae.CreateEncryptor(_keyEncrypt, _vectorBytes);
                _stream = new MemoryStream();
                _cryptStream = new CryptoStream(_stream, _transform, CryptoStreamMode.Write);

                _cryptStream.Write(_pwdBytes, 0, _pwdBytes.Length);
                _cryptStream.FlushFinalBlock();

                //Chuyen du lieu ma hoa sang kieu byte
                _tempEncrypt = _stream.ToArray();
            }
            finally
            {
                // Dong cac stream
                if (_cryptStream != null) _cryptStream.Close();
                if (_stream != null) _stream.Close();

                if (_rijndae != null) _rijndae.Clear();
            }

            return Convert.ToBase64String(_tempEncrypt);
        }
        #endregion

        /// <summary>
        /// Giai ma chuoi ma hoa
        /// </summary>
        /// <param name="_passwordCrypt"></param>
        /// <returns></returns>

        #region Decryptography
        public string mtd_Decrypt(string _passwordCrypt)
        {
            /*
             * Chuyen cac chuoi thanh kieu byte
             */            
            byte[] _vectorBytes = Encoding.ASCII.GetBytes(_vector);
            byte[] _satBytes = Encoding.ASCII.GetBytes(_satValue);
            byte[] _pwdBytes = Convert.FromBase64String(_passwordCrypt);

            /*
             * Tao ma key dung lam giai ma 
             */
            PasswordDeriveBytes _pwd = new PasswordDeriveBytes(_key, _satBytes, _hashAlgorithm, _interation);
            byte[] _keyEncrypt = _pwd.GetBytes(_keySize / 8);

            /*
             * Giai ma bang phuong phap RijndaelManaged 
             */
            byte[] _tempEncrypt = new byte[_pwdBytes.Length];

            RijndaelManaged _rijndae = null;
            MemoryStream _stream = null;
            CryptoStream _cryptStream = null;
            int _decryptCount = 0;
            try
            {
                _rijndae = new RijndaelManaged();
                _rijndae.Mode = CipherMode.CBC;
                ICryptoTransform _transform = _rijndae.CreateDecryptor(_keyEncrypt, _vectorBytes);
                _stream = new MemoryStream(_pwdBytes);
                _cryptStream = new CryptoStream(_stream, _transform, CryptoStreamMode.Read);

                
                //Chuyen du lieu giai ma sang kieu byte
                _decryptCount = _cryptStream.Read(_tempEncrypt, 0, _pwdBytes.Length);
                                
            }
            finally
            {
                // Dong cac stream
                if (_cryptStream != null) _cryptStream.Close();
                if (_stream != null) _stream.Close();

                if (_rijndae != null) _rijndae.Clear();
            }

            return Encoding.UTF8.GetString(_tempEncrypt, 0, _decryptCount);
        }
        #endregion


    }
}
